Ext.ns('Tm.notice');
Tm.notice.NoticeAddPanel = Ext.extend(Ext.Panel, {
			id : 'noticeAddPanel',
			frame : true,
			autoWidth : true,
			autoHeight : true,
			layout : 'fit',
			dataId : null,
			initComponent : function() {
				var dataId = this.dataId;
				var form = new Tm.common.FormPanel({
							frame : true,
							ref : 'form',
							defaults : {
								xtype : 'textfield',
								margins : '0 5 0 0',
								width : 300
							},
							errorReader : new Tm.common.DataReader(),
							items : [{
										name : 'id',
										hidden : true
									}, {
										xtype : 'googlesearchfield',
										fieldLabel : '案件',
										name : 'taskId',
										hiddenName : 'taskId',
										queryURL : 'task/goolgeTask.do',
										listeners : {
											render : function(c) {
												Ext.QuickTips.register({
															target : c,
															title : '帮助',
															text : '如需关联案例,请在此选择相关案例'
														});
											}
										}
									}, {
										name : 'title',
										fieldLabel : '标题',
										allowBlank : false
									}, {
										name : 'content',
										fieldLabel : '内容',
										xtype : 'textarea',
										allowBlank : false
									}, {
										fieldLabel : '提醒日期',
										xtype : 'datefield',
										name : 'executeTime',
										format : 'Y-m-d',
										altFormats : 'Y-m-d H:i:s',
										minValue : (new Date()).clearTime(),
										allowBlank : false
									}, {
										allowAddNewData : true,
										xtype : 'superboxselect',
										fieldLabel : '短信提醒',
										emptyText : '输入您将要提醒的手机号码(可以输入多个)',
										resizable : true,
										name : 'noReceiveMessages',
										width : 297,
										store : new Ext.data.JsonStore({
													url : 'notice/loadNoticeMobileList.do',
													autoLoad : true,
													fields : [{
																id : 'id'
															}, {
																name : 'name'
															}]
												}),
										// store : new Ext.data.SimpleStore({
										// fields : ['name'],
										// data : [[]],
										// sortInfo : {
										// field : 'name',
										// direction : 'ASC'
										// }
										// }),
										mode : 'local',
										displayField : 'name',
										valueField : 'name',
										listeners : {
											newitem : function(bs, v, f) {
												var newObj = {
													id : v,
													name : v
												};
												bs.addItem(newObj);
											},
											beforeadditem : function(superboxselect, value,recode) {
												var newValue=value;
												if(value.indexOf(':')!=-1){
													var startIndex=value.indexOf(':')+1;
													var endIndex=value.length;
													newValue=value.substring(startIndex,endIndex);
													superboxselect.addItem({id:newValue, name:newValue});
													return false;
												}else{
													return true;
												}
												
											}
										}
									}, {
										allowAddNewData : true,
										xtype : 'superboxselect',
										fieldLabel : 'email提醒',
										emptyText : '输入您将要提醒的email(可以输入多个)',
										resizable : true,
										name : 'noReceiveMails',
										width : 297,
										store : new Ext.data.JsonStore({
													url : 'notice/loadNoticeEmailList.do',
													autoLoad : true,
													fields : [{
																id : 'id'
															}, {
																name : 'name'
															}]
												}),
										mode : 'local',
										displayField : 'name',
										valueField : 'name',
										listeners : {
											newitem : function(bs, v, f) {
												var newObj = {
													id : v,
													name : v
												};
												bs.addItem(newObj);
											},
											beforeadditem : function(superboxselect, value,recode) {
												var newValue=value;
												if(value.indexOf(':')!=-1){
													var startIndex=value.indexOf(':')+1;
													var endIndex=value.length;
													newValue=value.substring(startIndex,endIndex);
													superboxselect.addItem({id:newValue, name:newValue});
													return false;
												}else{
													return true;
												}
												
											}
										}
									}, {
										name : 'status',
										fieldLabel : '状态',
										xtype : 'combo',
										mode : 'local',
										editable : false,
										typeAhead : true,
										forceSelection : true,
										hiddenName : 'status',
										mode : 'local',
										triggerAction : 'all',
										emptyText : '请选择...',
										selectOnFocus : true,
										store : Tm.data.noticeStates,
										allowBlank : false,
										readOnly : true
									}]
						});
				var toolBar = new Ext.Toolbar({
							hideBorders : true,
							buttons : [{
										text : '保存',
										iconCls : 'act_acceptIcon',
										handler : function() {
											if (form.getForm().isValid()) {
												var noticeAddPanel = Ext.getCmp('noticeAddPanel');
												var notice = Ext.apply(form.getForm().getFieldValues(false), {
															'executeTime' : form.getForm().findField('executeTime').value,
															'noReceiveMails' : null,
															'noReceiveMessages' : null
														})
												var noReceiveMails = noticeAddPanel.toReceiveArray(form, 'noReceiveMails');
												var noReceiveMessages = noticeAddPanel.toReceiveArray(form, 'noReceiveMessages');
												notice.noReceiveMails = noReceiveMails;
												notice.noReceiveMessages = noReceiveMessages;
												Ext.Ajax.request({
															waitMsg : '正在提交,请稍候...',
															url : 'notice/saveOrUpdateNotice.do',
															method : 'POST',
															headers : {
																'Content-Type' : 'application/json'
															},
															jsonData : Ext.encode(notice),
															success : function(response, action) {
																var notice = Ext.decode(response.responseText);
																App.setAlert('ok', '操作成功,此提醒将在' + notice.executeTime + '时发出');
																var form = noticeAddPanel.form;
																noticeAddPanel.init(notice, form);
															},
															failure : function(action, response) {
																App.setAlert('error', '操作失败');
															}
														});
											}
										}
									}, {
										xtype : 'tbseparator'
									}, {
										text : '重置',
										iconCls : 'act_resetIcon',
										handler : function() {
											form.getForm().reset();
										}
									}, {
										xtype : 'tbseparator'
									}, {
										text : '即时发送',
										iconCls : 'icon_send_notice',
										handler : function() {
											if (form.getForm().isValid()) {
												var noticeAddPanel = Ext.getCmp('noticeAddPanel');
												var notice = Ext.apply(form.getForm().getFieldValues(false), {
															'executeTime' : form.getForm().findField('executeTime').value,
															'noReceiveMails' : null,
															'noReceiveMessages' : null
														})
												var noReceiveMails = noticeAddPanel.toReceiveArray(form, 'noReceiveMails');
												var noReceiveMessages = noticeAddPanel.toReceiveArray(form, 'noReceiveMessages');
												notice.noReceiveMails = noReceiveMails;
												notice.noReceiveMessages = noReceiveMessages;
												Ext.Ajax.request({
															waitMsg : '正在提交,请稍候...',
															url : 'notice/sendNotice.do',
															method : 'POST',
															headers : {
																'Content-Type' : 'application/json'
															},
															jsonData : Ext.encode(notice),
															success : function(response, action) {
																var notice = Ext.decode(response.responseText);
																App.setAlert('ok', '操作成功,此提醒已经发出');
																var form = noticeAddPanel.form;
																noticeAddPanel.init(notice, form);
															},
															failure : function(action, response) {
																App.setAlert('error', '操作失败');
															}
														});
											}
										}
									}]
						});
				Ext.apply(this, {
							tbar : toolBar,
							items : [form]
						});
				Tm.notice.NoticeAddPanel.superclass.initComponent.call(this);
			},
			loadData : function() {
				var dataId = this.dataId;
				var noticeAddPanel = Ext.getCmp('noticeAddPanel');
				var form = noticeAddPanel.form;
				form.getForm().findField('status').setValue('RTS');
				if (dataId) {
					Ext.Ajax.request({
								url : 'notice/loadNotice.do',
								params : {
									id : dataId
								},
								success : function(response, action) {
									var notice = Ext.decode(response.responseText);
									noticeAddPanel.init(notice, form);
								},
								failure : function(response, action) {
									App.setAlert('error', '载入失败');
								}
							});
				}
			},
			init : function(notice, form) {
				var noticeAddPanel = Ext.getCmp('noticeAddPanel');
				var toolbarItems = noticeAddPanel.topToolbar.items.items;
				if (notice.status == 'SENT') {
					for (var i = 0; i < toolbarItems.length; i++) {
						toolbarItems[i].disable();
						form.getForm().findField('executeTime').minValue = null;
						form.getForm().findField('title').setReadOnly(true);
						form.getForm().findField('content').setReadOnly(true);
						form.getForm().findField('executeTime').setReadOnly(true);
						form.getForm().findField('noReceiveMessages').setReadOnly(true);
						form.getForm().findField('noReceiveMails').setReadOnly(true);
					}
				}
				form.getForm().findField('id').setValue(notice.id);
				form.getForm().findField('taskId').setValue(notice.taskId);
				form.getForm().findField('title').setValue(notice.title);
				form.getForm().findField('content').setValue(notice.content);
				form.getForm().findField('status').setValue(notice.status);
				form.getForm().findField('executeTime').setValue(notice.executeTime);
				noticeAddPanel.initSuperboxselect(form, 'noReceiveMessages', notice.noReceiveMessages);
				noticeAddPanel.initSuperboxselect(form, 'noReceiveMails', notice.noReceiveMails);
			},
			toReceiveArray : function(form, receiveName) {
				var noReceives = [];
				var noReceivesValue = form.getForm().findField(receiveName).getValue();
				if (noReceivesValue && noReceivesValue != '') {
					var valueArray = noReceivesValue.split(',');
					for (var i = 0; i < valueArray.length; i++) {
						noReceives[i] = {
							'status' : 'NEW',
							'value' : valueArray[i]
						}
					}
				}
				return noReceives;
			},
			initSuperboxselect : function(form, receiveName, noReceives) {
				var superboxselect = form.getForm().findField(receiveName);
				var valueObjectArray = [];
				for (var i = 0; i < noReceives.length; i++) {
					valueObjectArray[i] = {
						id : noReceives[i].value,
						name : noReceives[i].value
					}
				}
				superboxselect.setValueEx(valueObjectArray);
			},
			listeners : {
				render : function() {
					this.loadData();
				}
			}
		});
